﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using UI.Lib;
using Components;
using Controls;

namespace UI.ProductManagement
{
    public partial class FrmPot3a00Edit : UI.ParentForm.BaseEdit
    {
        public FrmPot3a00Edit(string action, string text, string key)
            : base(action)
        {
            InitializeComponent();

            this.Text = text;
            this.key = key;
            //回写的方法
            this.SaveCallFuntion = "Pot3a00Manager$SaveData"; //保存的方法
            if ("A" == this.Action)
            {
                IsAfterSaveCloseWin = false;
            }
            else
            {
                LoadData();
            }

            this.Size = base.MaxWindow;
            this.Width = 1000;
        }

        protected override bool Check()
        {
            if ("A" == this.Action)
            {
                System.Collections.Hashtable hs = new System.Collections.Hashtable();
                hs["productcode"] = this.tbSummary.GetValue("productcode");
                hs["%muser"] = UI.Lib.Context.GetAppKeyWhere();
                bool reflt = UI.Lib.Factory.CommonHelper.Entity.DataInData("Pot3a00", hs);
                if (reflt)
                {
                    MessageBox.Show("当前商品代码已经存在！", "提示");
                    this.tbSummary.Focus("productcode");
                    return false;
                }

                return true;
            }

            return true;
        }

        public override bool BeforePopWin(string name, ref string where, ref string[] others)
        {
            //对属性、类型、单位等进行过滤
            if ("levercode" == name)
            {
                where = string.Format("status ='1' {0}", Context.GetAppKeyWhere("muser", true));
            }
            if ("propty" == name
                || "um" == name || "levelcode" == name || "taxpct" == name)
            {
                where = Context.GetAppKeyWhere("muser");
            }
            else if ("pbrandcode" == name || "pclass" == name)
            {
                where = string.Format("status ='01' {0}", Context.GetAppKeyWhere("muser", true));
            }

            return true;
        }

        public override void AfterPopWin(string name, System.Collections.Hashtable data, bool ischange)
        {
            if ("calcode" == name)
            {
                string calcode = Validation.ToString(tbSummary.GetValue("calcode"));
                if ("2" == calcode) //移动成本 ，在收货时自动算出来
                {
                    this.tbSummary.SetValue("pamt", 0);
                    this.tbSummary.SetValue("pamtax", 0);

                    this.tbSummary.Enable("pamt", false);
                }
                else
                {
                    SetPamtByCalcode();
                }
            }
        }

        /// <summary>
        /// 根据成本计算方式的选择计算成本和含税成本
        /// </summary>
        void SetPamtByCalcode()
        {
            string calcode = Validation.ToString(tbSummary.GetValue("calcode"));
            if ("1" == calcode) //固定成本
            {
                //取得税率
                double taxp = Validation.ToDouble(tbSummary.GetValue("taxp"));

                if (this.tbSummary.IsEnabled("pamt") == false)
                {
                    //取得进价
                    double price = Validation.ToDouble(tbSummary.GetValue("price"));
                    //写成本
                    this.tbSummary.SetValue("pamt", price);
                    //含税成本
                    this.tbSummary.SetValue("pamtax", Validation.ToDouble(price + price * taxp, 3));
                }
                else
                {
                    double pamt = Validation.ToDouble(tbSummary.GetValue("pamt"));
                    //含税成本
                    this.tbSummary.SetValue("pamtax", Validation.ToDouble(pamt + pamt * taxp, 3));
                }

                this.tbSummary.Enable("pamt", true);
            }
        }

        /// <summary>
        /// 设置含税价格
        /// </summary>
        void SetBpricetax()
        {
            //取得售价
            double bprice = Validation.ToDouble(tbSummary.GetValue("bprice"));
            //取得税率
            double taxp = Validation.ToDouble(tbSummary.GetValue("taxp"));
            //含税成本
            this.tbSummary.SetValue("bpricetax", Validation.ToDouble(bprice + bprice * taxp, 3));
        }

        //加载修改数据
        private void LoadData()
        {
            base.LoadEditData(null, string.Format(" a.listid ='{0}' ", this.key));

            if (0 == Validation.ToInt(tbSummary.GetValue("bpricetax")))
            {
                SetBpricetax();
            }
        }

        protected override void AfterSummaryCreateControl(string colName, Control c, Controls.Args.SummaryArgs e)
        {
            if ("pamt" == colName || "bprice" == colName) //成本和售价改变后，更新毛利率
            {
                TextBox txt = c as TextBox;
                if (txt == null) return;

                txt.TextChanged += new EventHandler(txt_TextChanged);
            }
            else if ("barcode" == colName)
            {
                TextBoxX txt = c as TextBoxX;
                txt.ValueChanged += new TextBoxX.DgValueChanged(txt_ValueChanged);
            }
            else if ("taxp" == colName) //税率变化了，也会影响到计算
            {
                TextBoxX txt = c as TextBoxX;
                txt.ValueChanged += new TextBoxX.DgValueChanged(txtTexp_Changed);
            }
        }

        /// <summary>
        /// 税率改变时触发
        /// </summary>
        void txtTexp_Changed(object sender, EventArgs e)
        {
            txt_TextChanged(null, null); //重新计算毛利

            SetPamtByCalcode(); //可能含税的成本也将改变

            SetBpricetax();
        }

        /// <summary>
        /// 自动完成条形码的补全
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        void txt_ValueChanged(object sender, EventArgs e)
        {
            string barcode = Validation.ToString(this.tbSummary.GetValue("barcode"));
            if (!string.IsNullOrEmpty(barcode) && barcode.Length < 13)
            {
                this.tbSummary.SetValue("barcode", barcode.PadLeft(13, '0')); //让条形码成为13位
            }
        }

        void txt_TextChanged(object sender, EventArgs e)
        {
            //进价改变时，修改毛利率
            double pamt = Validation.ToDouble(this.tbSummary.GetValue("pamt")); //成本
            //售价
            double bprice = Validation.ToDouble(this.tbSummary.GetValue("bprice"));

            if (bprice == 0)
            {
                this.tbSummary.SetValue("pct", "0");
                return;
            }

            //计算毛利率 （售价-成本）/售价 //2011-07-23修改成本
            double pct = (bprice - pamt) / bprice;
            this.tbSummary.SetValue("pct", Validation.ToDouble(pct, 3));

            SetPamtByCalcode(); //重新计算含税的成本和单价

            SetBpricetax();
        }

        protected override void AfterSystemChangeControlState()
        {
            if ("M" == this.Action)
            {
                this.tbSummary.Enable("pamt", "1" == Validation.ToString(tbSummary.GetValue("calcode")));
            }
        }
    }
}